
import LoginView from '@/views/LoginView.vue';
import { createRouter, createWebHashHistory } from 'vue-router';
import Index from '@/views/index.vue';
import ProductInfo from '@/views/ProductInfo.vue';
import Publish from '@/views/Publish.vue';
import User from '@/views/user/user.vue';
const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { guestOnly: true }, // 标记只允许未登录用户访问
  },

  {
    path: '/',
    redirect: "/index",
   
  },
  
 
  {
    name: "index",
    path: "/index",
    component: Index,
    redirect: "/productInfo",
    children: [
      {
        path: "/productInfo",
        component: ProductInfo,
        name: "productInfo",
      },
      {
        path: "/followTrend",
        component: ProductInfo,
        name: "followTrend",
      },
      {
        path: "/notice",
        component: ProductInfo,
        name: "notice",
      },
      {
        path: "/user",
        component: User,
        name: "user",
      },
      {
        path: "/publish",
        component: Publish,
        name: "push",
      },
      {
        path: "/orderDetail",
        component: ProductInfo,
        name: "orderDetail",
      },
      {
        path: "/orderCreate",
        component: ProductInfo,
        name: "orderCreate",
      },
      {
        path: "/paymentPay",
        component: ProductInfo,
        name: "paymentPay",
      },
      {
        path: "/orderEvaluate",
        component: ProductInfo,
        name: "orderEvaluate",
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});

// 导航守卫
// router.beforeEach((to, from, next) => {
//   const isAuthenticated = localStorage.getItem('kuaikuai'); // 假设 token 存储在 localStorage 中
//   const isGuestRoute = to.matched.some(record => record.meta.guestOnly);
//   const isProtectedRoute = to.matched.some(record => record.meta.requiresAuth);

//   if (isGuestRoute && isAuthenticated) {
//     // 如果用户已登录且尝试访问仅限未登录用户的页面（如登录页）
//     next({ path: '/' }); // 重定向到主页
//   } else if (isProtectedRoute && !isAuthenticated) {
//     // 如果用户未登录且尝试访问需要认证的页面
//     next({ path: '/login' }); // 重定向到登录页
//   } else {
//     // 否则继续导航
//     next();
//   }
// });

export default router;